
package cn.com.dhcc.ddi.task.dao;

import java.util.List;

import cn.com.dhcc.app.core.dao.BaseDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.pub.CM;
import cn.com.dhcc.ddi.task.vo.TaskExecuteLog;
import cn.com.dhcc.ddi.task.vo.wapper.TaskLogRowWapper;
import cn.com.dhcc.ddi.task.vo.wapper.TaskRowWapper;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

/**
 * @类: TaskExecuteLogDao
 * @描述: 任务执行日志Dao
 * @作者: 张洪涛
 * @日期: 2016年11月14日 下午3:36:30
 * @版权所有: 东华软件
 * @版本:1.0
 */
@Repository
public class TaskExecuteLogDao extends BaseDao {
	private static Logger logger = Log.getLogger(LOGTYPE.DDI);

	/**
	 * 
	 * @方法: getLastExecuteLog
	 * @描述: 根据任务ID获取任务最后开始执行日志
	 * @param taskId 任务id
	 * @return
	 * @throws DaoException
	 * @throws
	 */
	public TaskExecuteLog getLastExecuteLog(String taskId) throws DaoException{
		String msg;
		String sql = "select * "
				  + "from " + SI_TABLE("t_ddi_task_log") + " t "
				  + "where t.linkstarttime = "
				  + "      (select distinct max(t1.linkstarttime) "
				  + "         from " + SI_TABLE("t_ddi_task_log") + " t1 "
				  + "        where t1.task_id = ?) and  task_id=? limit 1";
		try {
			return CM.getDao().getModule("根据任务ID获取任务最后开始执行日志", sql, TaskExecuteLog.class,new Object[]{taskId,taskId});
		} catch (DaoException e) {
			msg = "根据任务ID获取任务最后开始执行日志失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}

	/**
	 * 获取抽取日志
	 * @param taskLogId 任务主体日志
	 * @return
     */
	public TaskExecuteLog getExtractLog(String taskLogId) throws DaoException {
		String msg;
		String sql = "select * from t_ddi_task_log where group_id=? and LINKTYPE=?";
		try {
			return CM.getDao().getModule("",sql,TaskExecuteLog.class,new Object[]{taskLogId, TaskExecuteLog.LINK_TYPE.EXTRACT.getCode()});
		} catch (DaoException e) {
			msg = "根据任务ID获取任务最后开始执行日志失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}

	/**
	 * 获取抽取日志
	 * @param taskLogId 任务主体日志
	 * @return
	 */
	public TaskExecuteLog getTransformtLog(String taskLogId) throws DaoException {
		String msg;
		String sql = "select * from t_ddi_task_log where group_id=? and LINKTYPE=?";
		try {
			return CM.getDao().getModule("",sql,TaskExecuteLog.class,new Object[]{taskLogId, TaskExecuteLog.LINK_TYPE.TRANSFORM.getCode()});
		} catch (DaoException e) {
			msg = "根据任务ID获取任务最后开始执行日志失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
	/**
	 * 获取对象
	 * @param id
	 * @return
	 * @throws DaoException
	 */
	public TaskExecuteLog get(String id) throws DaoException {
		return getDao().getModuleById("获取对象",TaskExecuteLog.class,new Object[]{id});
	}
	public int insert(TaskExecuteLog taskExecuteLog) throws DaoException {
		return getDao().insertModule("新增任务执行日志",taskExecuteLog);
	}
	public int update(TaskExecuteLog taskExecuteLog) throws DaoException {
		return getDao().updateModuleByIdExecuteNull("修改任务执行日志",taskExecuteLog);
	}
	public int insertOrUpdate(TaskExecuteLog taskExecuteLog) throws DaoException {
		return getDao().saveOrUpdateModuleByIdExecuteNull("新增或修改任务执行日志",taskExecuteLog);
	}
	
	/**
     * 获取任务日志数
     * @param  id 任务日志id
     * @return 任务日志数
     * @throws DaoException
     */
    public int getCount(String id) throws DaoException{
		String msg = "";
		String sql = "select count(*) from "+SI_TABLE("t_ddi_task_log")+"  where task_id = ? and group_id IS NULL";
		try {
			Long taskID=getDao().queryForLong("", sql, new Object[]{id});
			return taskID.intValue();
		} catch (DaoException e) {
			msg = "获取表数据失败";
			throw new DaoException(msg, e);
		}
	}
    
    /**
     * 获取任务日志分页列表
     * @param pager
     * @param searchData
     * @return 任务日志分页列表
     * @throws DaoException
     */
    public PagerData<TaskExecuteLog> getTaskLogPagerList(Pager pager, WhereCondition searchData, String taskId) throws DaoException {
		String msg;
		String sql = "SELECT * FROM " + SI_TABLE("t_ddi_task_log") + " t1 WHERE t1.task_id = ? ";
					   
		try {
			return CM.getDao().getPagerList("", sql, new TaskLogRowWapper(),pager,searchData,new Object[]{taskId});
		} catch (DaoException e) {
			msg = "获取任务日志分页列表失败";
			throw new DaoException(msg, e);
		}
	}
    
    /**
     * 获取子任务日志
     * @param id 主任务id
     * @param searchData
     * @return 子任务日志列表
     * @throws DaoException
     */
    public List<TaskExecuteLog> getSubTaskByGroupId(String id, WhereCondition searchData) throws DaoException {
    	String msg = "";
    	String sql = "SELECT * FROM " + SI_TABLE("t_ddi_task_log") + " t1 WHERE t1.group_id = ? ";
		try {	
			return getDao().list("获取周期性任务", sql, new TaskLogRowWapper(),searchData,new Object[]{id});
		} catch (DaoException e) {
			msg = "获取子任务日志失败 ";
			throw new DaoException(msg, e);
		}
    }
    
    /**
     * 根据任务id删除任务日志
     * @param taskId 任务id
     * @return 成功数
     * @throws DaoException 
     */
    public int deleteByTaskId(String taskId) throws DaoException {
    	try {
    		String sql = "DELETE FROM " + SI_TABLE("t_ddi_task_log") + " WHERE task_id=?";
			return getDao().delete("根据任务id删除任务日志", sql, new Object[]{taskId});
		} catch (DaoException e) {
			throw new DaoException("删除任务日志失败", e);
		}
    }

	/**
	 * 获取最后有数据发送日志
	 * @param taskId
	 * @return
	 * @throws DaoException
	 */
	public TaskExecuteLog getLastDataExecuteLog(String taskId) throws DaoException{
		String msg = "";
		String sql = "select * "
				+" from " + SI_TABLE("t_ddi_task_log")+" t"
				+" where t.execute_begin_time ="
				+"   (select distinct max(t1.execute_begin_time)"
				+"       from " + SI_TABLE("t_ddi_task_log")+" t1"
				+"       where t1.task_id = ? and t1.LINKTYPE is null and t1.group_id is null"
				+"      and t2.total>0)"
				+"   and task_id = ?";
		try {
			return CM.getDao().get("根据任务ID获取任务最后开始执行日志", sql, new TaskLogRowWapper(),new Object[]{taskId,taskId});
		} catch (DaoException e) {
			msg = "根据任务ID获取任务最后开始执行日志失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}

	public List<TaskExecuteLog> getSendingTaskExecuteLog(String taskId) throws DaoException{
		String msg = "";
		String sql = "select * from  "+SI_TABLE("t_ddi_task_log")+" where TASK_ID=?  and LOG_STATUS is null order by LINKSTARTTIME desc";
		try {
			List<TaskExecuteLog> taskExecuteLogList=CM.getDao().list("", sql, new TaskLogRowWapper(),new Object[]{taskId});
			if(taskExecuteLogList.size()!=0){
				return taskExecuteLogList;
			}else{
				return null;
			}
		} catch (DaoException e) {
			msg = "获取表数据失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}

	/**
	 * 获取任务环节日志
	 * @param groupId 大任务id
	 * @param linktype 环节类型
	 * @return
	 * @throws DaoException
     */
	public TaskExecuteLog getLinkLog(String groupId, String linktype) throws DaoException {
		String msg = "";
		String sql = "select * from  "+SI_TABLE("t_ddi_task_log")+" where group_id=?  and linktype = ? ";
		try {
			return  CM.getDao().get("", sql, new TaskLogRowWapper(),new Object[]{groupId,linktype});
		} catch (DaoException e) {
			msg = "获取任务环节日志失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}

    public List<TaskExecuteLog> getFailTaskLog(String taskId) throws DaoException {
		String msg = "";
		String sql = "select * from  "+SI_TABLE("t_ddi_task_log")+" where task_id=?  and log_status = 0 ";
		try {
			return  CM.getDao().list("", sql, new TaskLogRowWapper(),new Object[]{taskId});
		} catch (DaoException e) {
			msg = "获取任务环节日志失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
    }
}
